package com.cg.controller;

import com.cg.pojo.Result;
import com.cg.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String>upload(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
       String originalFile = file.getOriginalFilename();
       //保证文件的文字是唯一的，防止文件覆盖

        String filename= UUID.randomUUID().toString()+originalFile.substring(originalFile.lastIndexOf("."));
//        file.transferTo(new File("D:\\Yworkspace\\files\\"+filename));
       String url= AliOssUtil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
